package com.chuji.crmcj.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.SalesAdapter;
import com.chuji.crmcj.bean.AgreeOrderInfo;
import com.chuji.crmcj.bean.CancelUserListInfo;
import com.chuji.crmcj.bean.ConfirmRejectInfo;
import com.chuji.crmcj.bean.MagClientLoseDetailInfo;
import com.chuji.crmcj.bean.MagClientLoseInfo;
import com.chuji.crmcj.utils.CommonUtil;
import com.chuji.crmcj.utils.Jump;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MagClientLoseDetailActivity extends BaseActivity implements View.OnClickListener{
    //回退
    private ImageView mIv_Back;
    //数据存储
    List<AgreeOrderInfo.ApiParamObjEntity> agreeOrderData;
    //网络数据
    private AgreeOrderInfo agreeOrderInfo;
    //同意战败存储
    List<MagClientLoseDetailInfo.ApiParamObjEntity> magClientLoseDetailData;
    //同意战败网络数据
    private MagClientLoseDetailInfo magClientLoseDetailInfo;
    //客户头像
    private ImageView mIv_client_head;
    //客户姓名
    private TextView mTv_client_name;
    //客户电话
    private TextView mTv_client_phoneNumber;
    //意向车型
    private TextView mTv_car_type;
    //确认战败
    private LinearLayout ll_redeal;
    //驳回
    private LinearLayout ll_reject;
    //驳回人员列表存储
    List<CancelUserListInfo.ApiParamObjEntity> cancelUserListData;
    //驳回人员列表网络数据
    private CancelUserListInfo cancelUserListInfo;
    //确认驳回存储
    List<ConfirmRejectInfo.ApiParamObjEntity> confirmRejectData;
    //确认驳回网络数据
    private ConfirmRejectInfo confirmRejectInfo;
    //客户等级
    private ImageView mIv_level;
    //销售员名字
    private TextView mTv_sales_name;
    //最后联系时间
    private TextView mTv_last_time;
    //战败原因内容
    private TextView mTv_lose_reason;
    //备注内容
    private TextView mTv_remarks;
    //显示列表
    private ListView lv_checkBox;
    private SalesAdapter adapter;
    //集合
    private ArrayList<String>  list= new ArrayList<String>();
    //意图
    Intent intent;
    //战败ID
    String DefeatApproveID;
    //用户ID
    String UserID;
    //销售员ID
    String SalesID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mag_client_lose_detail);
        //初始化组件
        mIv_Back = (ImageView) findViewById(R.id.iv_Back);
        mIv_client_head = (ImageView) findViewById(R.id.iv_client_head);
        mTv_client_name = (TextView) findViewById(R.id.tv_client_name);
        mTv_client_phoneNumber = (TextView) findViewById(R.id.tv_client_phoneNumber);
        mTv_car_type = (TextView) findViewById(R.id.tv_car_type);
        ll_redeal = (LinearLayout) findViewById(R.id.ll_redeal);
        ll_reject = (LinearLayout) findViewById(R.id.ll_reject);
        mIv_level = (ImageView) findViewById(R.id.iv_level);
        mTv_sales_name = (TextView) findViewById(R.id.tv_sales_name);
        mTv_last_time = (TextView) findViewById(R.id.tv_last_time);
        mTv_lose_reason = (TextView) findViewById(R.id.tv_lose_reason);
        mTv_remarks = (TextView) findViewById(R.id.tv_remarks);
        //数据
        initDa();
        //设置点击事件
        mIv_Back.setOnClickListener(this);
        ll_redeal.setOnClickListener(this);
        ll_reject.setOnClickListener(this);
        //获取战败申请ID
        intent=getIntent();
        DefeatApproveID=intent.getStringExtra("DefeatApproveID");
        //得到用户ID
        UserID=SPUtils.getString(UIUtils.getContext(),"UserID", "");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    public void initDa() {
        getAppraiseData();
    }
    private void getAppraiseData() {
        //第一次
        if (magClientLoseDetailData == null) {
            magClientLoseDetailData = new ArrayList<MagClientLoseDetailInfo.ApiParamObjEntity>();
        }
        requestData();
    }
    private void requestData() {
        //初始化Volley
        String strURL = String.format(UrlUtils.GET_MAG_CLIENT_LOSE_DETAIL+"&UserID="+UserID+"&defeatType=1"+"&DefeatApproveID="+DefeatApproveID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                magClientLoseDetailInfo = JSON.parseObject(response, MagClientLoseDetailInfo.class);
                magClientLoseDetailData.addAll(magClientLoseDetailInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i=0;i<magClientLoseDetailData.size();i++) {
                            mTv_client_name.setText(magClientLoseDetailData.get(i).getCustomerName());
                            mTv_car_type.setText(magClientLoseDetailData.get(i).getCarModel());
                            if(magClientLoseDetailData.size()==11) {
                                mTv_client_phoneNumber.setText(NumberUtils.parsePhoneNumber(magClientLoseDetailData.get(i).getCustormerTel()));
                            }else{
                                mTv_client_phoneNumber.setText(magClientLoseDetailData.get(i).getCustormerTel());
                            }
                            mTv_sales_name.setText(magClientLoseDetailData.get(i).getSaleName());
                            mTv_last_time.setText(magClientLoseDetailData.get(i).getCreateTime());
                            mTv_lose_reason.setText(magClientLoseDetailData.get(i).getDefeatReason());
                            mTv_remarks.setText(magClientLoseDetailData.get(i).getDefeatcomment());
                            //判断客户等级
                            mIv_level.setImageDrawable(CommonUtil.getLevelIcon(magClientLoseDetailData.get(i).getCustomerLevel()));
                        }
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.ll_redeal:
                showLoseClientDialog();
                break;
            case R.id.ll_reject:
                showSalesDialog();
                break;
            case R.id.iv_Back:
                finish();
                break;
        }
    }
    //自定义dialog
    private void showLoseClientDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_mag_client_lose, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        final TextView mTvFolType = (TextView) view.findViewById(R.id.tv_isfollow);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //初始化Volley
                String strURL = String.format(UrlUtils.AGREE_ORDER+"&UserID="+UserID+"&DefeatApproveID="+DefeatApproveID);
                StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        agreeOrderInfo = JSON.parseObject(response, AgreeOrderInfo.class);
                        //第一次
                        if (agreeOrderData == null) {
                            agreeOrderData = new ArrayList<AgreeOrderInfo.ApiParamObjEntity>();
                        }
                        agreeOrderData.addAll(agreeOrderInfo.getApiParamObj());
                        UIUtils.runInMainThread(new Runnable() {
                            @Override
                            public void run() {
                                if(agreeOrderData.get(0).getIsok()==1){
                                    UIUtils.showToastSafe("已确认战败");
                                }
                            }
                        });
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        System.out.println("失败" + error);
                        UIUtils.showToastSafe("请检查网络");
                    }
                });
                // Add the request to the RequestQueue.
                VolleyUtil.getRequestQueue().add(stringRequest);
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    private void showSalesDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_reject_apply, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);

        lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
        lv_checkBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        //初始化Volley
        String strURL = String.format(UrlUtils.GET_MAG_REJECT_SALES+"&UserID="+UserID+"&DefeatApproveID="+DefeatApproveID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        cancelUserListInfo = JSON.parseObject(response, CancelUserListInfo.class);
                        //第一次
                        if (cancelUserListData == null) {
                            cancelUserListData = new ArrayList<CancelUserListInfo.ApiParamObjEntity>();
                        }
                        cancelUserListData.addAll(cancelUserListInfo.getApiParamObj());
                        adapter=new SalesAdapter(UIUtils.getContext(), cancelUserListData);
                        lv_checkBox.setAdapter(adapter);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        System.out.println("失败" + error);
                        UIUtils.showToastSafe("请检查网络");
                    }
                });
                // Add the request to the RequestQueue.
                VolleyUtil.getRequestQueue().add(stringRequest);

        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //得到选中的数据并发送
                SalesID=adapter.getSalesID();
                String strURL = String.format(UrlUtils.GET_MAG_SURE_CANCEL_DEFEAT+"&UserID="+UserID+"&DefeatApproveID="+DefeatApproveID+"&SalesID="+SalesID);
                StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        confirmRejectInfo = JSON.parseObject(response, ConfirmRejectInfo.class);
                        //第一次
                        if (confirmRejectData == null) {
                            confirmRejectData = new ArrayList<ConfirmRejectInfo.ApiParamObjEntity>();
                        }
                        confirmRejectData.addAll(confirmRejectInfo.getApiParamObj());
                        UIUtils.runInMainThread(new Runnable() {
                            @Override
                            public void run() {
                                if(confirmRejectData.size()!=0&&confirmRejectData.get(0).getIsok()=="1"){
                                    UIUtils.showToastSafe("驳回成功");
                                }else{
                                    UIUtils.showToastSafe("驳回失败");
                                }
                            }
                        });

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        System.out.println("失败" + error);
                        UIUtils.showToastSafe("请检查网络");
                    }
                });
                // Add the request to the RequestQueue.
                VolleyUtil.getRequestQueue().add(stringRequest);
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
